package at.brandl.wwi;

import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;

public class WhereWasISelectionActivity extends Activity {

	static final String START = "start";
	static final String END = "end";
	protected static final int START_DATE_DIALOG_ID = 0;
	private static final int END_DATE_DIALOG_ID = 1;
	private static final long LOOK_BACK_PERIOD = 1000 * 60 * 60;
	private EditText beginDateField;
	private EditText endDateField;
	private Button beginDateButton;
	private Button endDateButton;
	private OnDateSetListener startDateSetListener = new OnDateSetListener() {
		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			startYear = year;
			startMonth = monthOfYear;
			startDay = dayOfMonth;
			updateDisplay();
		}
	};
	private OnDateSetListener endDateSetListener = new OnDateSetListener() {
		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			endYear = year;
			endMonth = monthOfYear;
			endDay = dayOfMonth;
			updateDisplay();
		}
	};
	private int startYear;
	private int startMonth;
	private int startDay;
	private int endYear;
	private int endMonth;
	private int endDay;
	private Button showInMapButton;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.selection);

		startService(new Intent(this, WhereWasIService.class));
		
		initGuiElements();
		
		setInitialValues(savedInstanceState);

		updateDisplay();
	}

	private void initGuiElements() {
		beginDateField = (EditText) this.findViewById(R.id.beginDate);
		beginDateButton = (Button) findViewById(R.id.beginDateButton);
		beginDateButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				showDialog(START_DATE_DIALOG_ID);
			}
		});

		endDateField = (EditText) this.findViewById(R.id.endDate);
		endDateButton = (Button) findViewById(R.id.endDateButton);
		endDateButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				showDialog(END_DATE_DIALOG_ID);
			}
		});

		showInMapButton = (Button) findViewById(R.id.showMap);
		showInMapButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(getApplicationContext(),
						WhereWasIMapActivity.class);
				intent.putExtra(START, getStartTime());
				intent.putExtra(END, getEndTime());
				startActivity(intent);
			}
		});
	}

	private Long getEndTime() {
		return Long.valueOf(createDate(endYear, endMonth, endDay, 23, 59)
				.getTimeInMillis());
	}

	private Long getStartTime() {
		return Long.valueOf(createDate(startYear, startMonth, startDay, 0, 0)
				.getTimeInMillis());
	}

	private void setInitialValues(Bundle savedInstanceState) {
		Calendar startDate = Calendar.getInstance();
		Calendar endDate = Calendar.getInstance();
		if (savedInstanceState != null) {
			startDate.setTimeInMillis(savedInstanceState.getLong(START));
			endDate.setTimeInMillis(savedInstanceState.getLong(END));
		} else {
			startDate.setTimeInMillis(System.currentTimeMillis()
					- LOOK_BACK_PERIOD);
			endDate.setTimeInMillis(System.currentTimeMillis());
		}
		startYear = startDate.get(Calendar.YEAR);
		startMonth = startDate.get(Calendar.MONTH);
		startDay = startDate.get(Calendar.DAY_OF_MONTH);
		endYear = endDate.get(Calendar.YEAR);
		endMonth = endDate.get(Calendar.MONTH);
		endDay = endDate.get(Calendar.DAY_OF_MONTH);
	}

	private void updateDisplay() {
		Calendar startDate = createDate(startYear, startMonth, startDay, 0, 0);
		beginDateField.setText(DateUtils.formatDateTime(this,
				startDate.getTimeInMillis(), DateUtils.FORMAT_24HOUR
						& DateUtils.LENGTH_SHORTEST));
		Calendar endDate = createDate(endYear, endMonth, endDay, 23, 59);
		endDateField.setText(DateUtils.formatDateTime(this,
				endDate.getTimeInMillis(), DateUtils.FORMAT_24HOUR
						& DateUtils.LENGTH_SHORTEST));
	}

	private Calendar createDate(int year, int month, int day, int hour, int minute) {
		Calendar endDate = Calendar.getInstance();
		endDate.set(year, month, day, hour, minute);
		return endDate;
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putLong(START, createDate(startYear, startMonth, startDay, 0, 0)
				.getTimeInMillis());
		outState.putLong(END, createDate(endYear, endMonth, endDay, 23, 59)
				.getTimeInMillis());
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case START_DATE_DIALOG_ID:
			return new DatePickerDialog(this, startDateSetListener, startYear,
					startMonth, startDay);
		case END_DATE_DIALOG_ID:
			return new DatePickerDialog(this, endDateSetListener, endYear,
					endMonth, endDay);
		}
		return null;
	}

}
